package org.flynenu.ums.logic.impl;

import org.flynenu.core.dao.CommonDAO;
import org.flynenu.ums.common.entity.base.User;
import org.flynenu.ums.common.ticket.Ticket;
import org.flynenu.ums.common.ticket.TicketCache;
import org.flynenu.ums.common.ticket.TicketsCacheImpl;
import org.flynenu.ums.logic.IPolice;

public class PoliceImpl implements IPolice {

	private volatile TicketCache stor = TicketsCacheImpl.instance(); // BeansManager.generateBean(BeansEnum.TicketsCache);//LogicFactory.getTicketStore();

	private CommonDAO commonDao = null;

	public void setCommonDao(CommonDAO commonDao) {
		this.commonDao = commonDao;
	}
	
	@Override
	public boolean surveyLogined(int uid, String ticketNo) {
		if(stor == null){
			throw new RuntimeException("the Ticket Cache is null!");
		}
		
		Ticket ticket = stor.queryTicketNo(uid);
		if (ticket == null) {
			return false;
		}

		String _ticketNo = ticket.getTicketNo();

		if (_ticketNo == null || _ticketNo.trim().equals("")) {
			return false;
		}

		return ticketNo.equals(_ticketNo);
	}

	@Override
	public boolean surveyUser(int uid) {
		User user = commonDao.queryById(1, User.class);

		return user != null;
	}
	
	public void backupTciket(Ticket ticket){
		if(stor == null){
			throw new RuntimeException("the Ticket Cache is null!");
		}
		
		stor.saveTicketNo(ticket);
	}

}
